package com.user.query.pay.controller;
import com.user.query.controller.BaseController;
import com.user.query.model.User;
import com.user.query.pay.service.PayInfoService;
import com.user.query.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/pay")
public class WXController extends BaseController {

    @Resource
    private PayInfoService paysInfoService;


    /***
     * 订单
     * @param request
     * @param map
     * @return
     */
    @RequestMapping("/dd")
    public String dd(HttpServletRequest request, String id, String sourceId, ModelMap map) {

        return "pay/pay";
    }
    /***
     * 订单
     * @param request
     * @param map
     * @return
     */
    @RequestMapping("/success")
    public String success(HttpServletRequest request, ModelMap map) {
        String orderId = request.getParameter("orderId");

        return "pay/success";
    }
    /**
     * 跳转收银台
     * @return
     */
    @RequestMapping("toPay")
    public String toPay(HttpServletRequest request,ModelMap map){
        String orderId = request.getParameter("oid");
        String imgPath = paysInfoService.wxpay(orderId);

        map.put("orderId", orderId);
        return "pay/topay";
    }
    /**
     * 继续支付
     * @param request
     * @param map
     * @return
     */
    @RequestMapping("rePay")
    public String rePay(HttpServletRequest request,ModelMap map){
        String orderId = request.getParameter("oid");

        map.put("orderId", orderId);
        return "pay/topay";
    }

    /**
     * 支付二维码
     * @param request
     * @return
     */
    @RequestMapping("getewm")
    @ResponseBody
    public Map<String,Object> getEwm(HttpServletRequest request){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("code", "400");
        String orderId = request.getParameter("orderId");
        String imgPath = paysInfoService.wxpay(orderId);
        if(StringUtils.isNotBlank(imgPath)){
            map.put("code", "200");
            map.put("imgPath", imgPath);
        }
        return map;
    }  /**
     * 支付二维码
     * @param request
     * @return
     */
    @RequestMapping("test")
    @ResponseBody
    public Map<String,Object> test(HttpServletRequest request){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("code", "400");


        //paysInfoService.verify2(order);
        return map;
    }

    /***
     * 是否支付成功
     * @param request
     * @return
     */
    @RequestMapping("issucess")
    @ResponseBody
    public Map<String,Object> issucess(HttpServletRequest request){
        HashMap<String,Object> map = new HashMap<String,Object>();
        String msg="验证失败";
        map.put("code", 400);
        String orderId = request.getParameter("orderId");
        int result = paysInfoService.verify(orderId);
        if(result==1){
            map.put("code", 200);
            msg="验证成功";
        }
        log.error("#####################################################"+msg);
        map.put("msg", msg);
        return map;
    }
    /***
     * 支付回调
     */
    @RequestMapping("/weixinNotify")
    public void wxnotify(HttpServletRequest request,HttpServletResponse response){
        paysInfoService.wxnotify(request,response);
    }
}
